//
//  CPYFavouriteViewController.m
//  TTMusic
//
//  Created by chenpengyu on 16/4/7.
//  Copyright © 2016年 cpy. All rights reserved.
//

#import "CPYFavouriteViewController.h"
#import "CPYFavouriteModel.h"
#import "CPYFavoriteTableViewCell.h"
#import "CPYSONGSModel.h"
@interface CPYFavouriteViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) TTMusicBaseTableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) UIImageView *titleImage;
@property (nonatomic, strong) CPYSONGSModel *songsModel;
@end

@implementation CPYFavouriteViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataSource = [NSMutableArray array];
    [self creatTableView];
    [self creatData];
    // Do any additional setup after loading the view.
}
- (void)creatNavButtonItem {
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]  initWithTitle:@"< 返回" style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonAction:)];
    [self.navigationItem.leftBarButtonItem setTintColor:[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0]];
}
- (void)leftBarButtonAction:(UIBarButtonItem *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)creatTableView {
    self.tableView = [[TTMusicBaseTableView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, SHORTHEIGHT) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
//    _tableView.rowHeight = 60 * FitHeight;
    _tableView.contentInset = UIEdgeInsetsMake(ImageHeight * FitHeight, 0, 0, 0);
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.titleImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, -ImageHeight * FitHeight, self.view.frame.size.width, ImageHeight * FitHeight)];
//    [_titleImage sd_setImageWithURL:[NSURL URLWithString:self.imageStr]];
    _titleImage.userInteractionEnabled = YES;
    _titleImage.contentMode = UIViewContentModeScaleAspectFill;
    [_tableView addSubview:_titleImage];
    [_tableView registerClass:[CPYFavoriteTableViewCell class] forCellReuseIdentifier:@"CPYFavoriteTableViewCellIdentifier"];
    [self.view addSubview:_tableView];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    //    NSLog(@"%f", _tableView.contentOffset.y);
    CGFloat y = _tableView.contentOffset.y;
    if (y < -ImageHeight) {
        CGRect frame = _titleImage.frame;
        frame.origin.y = y;
        frame.size.height = -y * FitHeight;
        _titleImage.frame = frame;
    }
    return;
}
- (void)creatData {
    [TTMusicAFNetworkTool GETNetWithUrl:[NSString stringWithFormat:@"http://api.songlist.ttpod.com/songlists/%@", self.songListId] body:nil headerFile:nil response:TTMusicJSON success:^(id result) {
        CPYFavouriteModel *finalSongListModel = [[CPYFavouriteModel alloc] initWithDataSource:result];
        self.songsModel = [[CPYSONGSModel alloc] initWithDataSource:result];
        [_dataSource addObject:finalSongListModel];
        [self.tableView reloadData];
        [_titleImage sd_setImageWithURL:[NSURL URLWithString:[((NSDictionary *)finalSongListModel.image) objectForKey:@"pic"]]];
        //        [MBProgressHUD hideHUD];
    } failure:^(NSError *error) {
        //        [MBProgressHUD showError:@"/加载失败"];
    }];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (_dataSource.count != 0) {
        CPYFavouriteModel *finalSongListModel = _dataSource[section];
        return finalSongListModel.songs.count;
    }else {
        return 0;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_dataSource.count != 0) {
        CPYFavoriteTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CPYFavoriteTableViewCellIdentifier"];
        CPYFavouriteModel *finalSongListModel = _dataSource[indexPath.section];
        cell.songsModel = finalSongListModel.songs[indexPath.row];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        return cell;
    }else {
        return nil;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80 * FitHeight;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [TTMusicAVPlayer shareTTMusicAVPlayer].local = indexPath.row;
    [[TTMusicAVPlayer shareTTMusicAVPlayer] playWithURL:_songsModel];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
